// pages/user/user.js
import {
    getReLogin,
    getNavigationbar
} from "../../api/index.js"
import {getShopAuthApi} from "../../api/user.js"
import {
    getOrderList
} from "../../api/order.js"
Page({
    options: {
        styleIsolation: "shared"
    },
    /**
     * 页面的初始数据
     */
    data: {
        obj: {},
        cellList: [{
            title: "我的订单",
            icon: "/images/icon／icon_order@2x.png",
            value: ''
        }, {
            title: "退款/售后",
            icon: "/images/icon／icon_tksh@2x.png",
        }, {
            title: "优惠券",
            icon: "/images/icon／icon_coupon@2x.png",
            value: ''
        }, {
            title: "收货地址",
            icon: "/images/icon／icon_address@2x.png",
        }, {
            title: "店铺认证",
            icon: "/images/icon／icon_store@2x.png",
            value: 0 // 0-已认证1-审核中2-认证未通过
        }, {
            title: "联系客服",
            icon: "/images/icon／icon_customer service@2x.png",
        }],
        isShow: false,
        noticeList: [
            {
                icon: "",
                title: "新店铺未审核",
                authState: 0,
                background: "#FFF8C5",
                color: "#262626"
            }, {
                icon: "/images/icon/icon／icon_warn@2x.png",
                title: "审核中",
                authState: 1,
                background: "#3facff",
                color: "#FFF"
            },
            {
                icon: "/images/icon/icon／icon_wrong@2x.png",
                title: "认证未通过",
                authState: 2,
                background: "#f30000",
                color: "#FFF"
            },
           
            {
                icon: "/images/icon/icon／icon_tick@2x.png",
                title: "认证成功",
                authState: 3,
                background: "#FFC107",
                color: "#FFF8C5"
            },
           
            {
              icon: "/images/icon/icon／icon_wrong@2x.png",
              title: "认证成功，重新认证审核不通过，原因：",
              authState: 4,
              background: "#FFDFDE",
              color: "#FFF"
          },
            {
              icon: "/images/icon/icon／icon_tick@2x.png",
              title: "已认证",
              authState: 5,
              background: "#FFC107",
              color: "#FFF"
          },
        ],
        authState:0
    },
    getSho() {
        getShopAuthApi({
            act: 'licence_info_new',
          }).then(res => {
            console.log('是是是',res)
            let status = res.status;
            let verify = res.verify;
            console.log("verifyverifyverify",verify)
            if (verify && verify.verify_status) {
              //待审核，看是否已经撤销
              if (verify.verify_status == 0) {
                let is_submit = verify.is_submit;
                if (is_submit == 0) {
                  if (status == 5) {
                    //认证成功同时有新材料待审核
                    this.setData({
                      authState: 5,
                      disabled: true
                    })
                    //认证详情页面展示verify里面的数据
                   
                  } else {
                    //待审核
                    this.setData({
                      authState: 1,
                      disabled: true
                    })
                    //认证详情页面展示verify里面的数据
                 
                  }
                } else {
                  if (status == 5) {
                    //认证成功
                    this.setData({
                      authState: 3,
                      disabled: true
                    })
                    //认证详情页面展示dic的数据
                    
                  } else if (status == 1) {
                    //认证失败
                    this.setData({
                      authState: 2,
                      disabled: true
                    })
                    //认证详情页面展示dic的数据
                    
                  } else {
                    //未提交
                    this.setData({
                      authState: 0,
                      disabled: false
                    })
                    //认证详情页面展示dic的数据
                    
                  }
                }
              } else {
                if (status == 5) {
                  if (verify.verify_status == 1) {
                    //"认证成功，重新认证审核不通过，原因：\(verify["reason"])"
                    this.setData({
                      authState: 4,
                      disabled: true
                    })
                    //认证详情页面展示verify里面的数据
                   
                  } else {
                    //认证成功
                    this.setData({
                      authState: 3,
                      disabled: false
                    })
                    //认证详情页面展示dic的数据
                 
                  }
                } else if (status == 1) {
                  //认证失败
                  this.setData({
                    authState: 2,
                    disabled: false
                  })
                  if (verify.verify_status == 1) {
                    //认证详情页面展示verify里面的数据
              
                  } else {
                    //认证详情页面展示dic的数据
                
                  }
                } else {
                  //未提交
                  this.setData({
                    authState: 0,
                    disabled: false
                  })
                  //认证详情页面展示dic的数据
        
                }
              }
            } else {
              if (status == 5) {
                //认证成功
                this.setData({
                  authState: 3,
                  disabled: true
                })
                //认证详情页面展示dic的数据
                
              } else if (status == 1) {
                //认证失败
                this.setData({
                  authState: 2,
                  disabled: false
                })
                //认证详情页面展示dic的数据
                
              } else {
                //未提交
                this.setData({
                  authState: 0,
                  disabled: false
                })
                //认证详情页面展示dic的数据
                
              }
            }
          })
    },
  
    getData() {
        getOrderList({
            act: 'orderlist',
            type: "all",
            p: 1,
            rollpage: 1000,
        }).then(res => {
            this.data.cellList.forEach(element => {
                if (element.title == '我的订单') {
                    element.value = res.length
                }
            });

            this.setData({
                cellList: this.data.cellList
            })
        })
        getNavigationbar({
            act: 'coupon_list',
        }).then(res => {
            console.log('e', res)
            this.data.cellList.forEach(element => {
                if (element.title == '优惠券') {
                    element.value = res.length
                }
            });
            this.setData({
                cellList: this.data.cellList
            })
        })

    },
    /**
     * 用户单元格跳转回调函数方法
     * @param {*} e 
     */
    handleCellChange(e) {
        console.log('e111111111', e)
        let cellItem = e.currentTarget.dataset.item || e.target.dataset.item // 获取点击单元格对象信息
        // 判断当前店铺是否完成认证，反之则弹出认证提示信息
        // wx.login({
        //     success: res => { 
        //         console.log('res',res)
        //     } })
        console.log('ce', )
        switch (cellItem.title) {
            case '我的订单':
                wx.navigateTo({
                    url: '/pages/order_list/index',
                })
                break;
            case '退款/售后':
                wx.navigateTo({
                    url: '/pages/order_refund_after_sales/index',
                })
                break;
            case '优惠券':
                wx.navigateTo({
                    url: '/pages/coupon/index',
                })
                break;
            case '收货地址':
                wx.navigateTo({
                    url: '/pages/address_management/index',
                })
                break;
            case '店铺认证':
                if(this.data.obj.isoriginaladmin == 1 && this.data.obj.parentid == 0) {
                    wx.navigateTo({
                        url: '/pages/shop_auth/index',
                    })
                } else {
                    getApp().Tips({ title: '创始人才能操作！' 
                })
                }
                
                break;
            case '联系客服':
                wx.makePhoneCall({
                    phoneNumber: "18377766475", //仅为示例，并非真实的电话号码
                    success: function () {
                        console.log("拨打电话成功！");
                    },
                    fail: function () {
                        console.log("拨打电话失败！");
                    },
                });
                break;
        }
    },
    getInfo() {
        let data = {
            act: 'login',
            sys_type: 'app',
            phone: '17776642313', // 15600000000
            password: '123456',
            client_device: 'wechat_xcx'
        }
        getReLogin(data).then(res => {
            console.log('返回', res)
            // 修改全局变量中的数据

        })
    },
    /**
     * 前往认证店铺回调函数方法
     */
    handleToAuth() {
        // 关闭弹窗-前往认证
        this.setData({
            isShow: false
        })
        wx.navigateTo({
            url: '/pages/shop_auth/index',
        })
    },
    getShop() {
        getShopAuthApi({
            act: 'licence_info_new',
        }).then(res => {
            console.log('res', res)
        })
    },

    /**
     * 关闭提示弹窗回调函数方法
     */
    handleClose() {
        this.setData({
            isShow: false
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      
        this.getData();
        this.getShop();
        this.getSho()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let res = wx.getStorageSync('USER')
        console.log('setStorageSync', res)
        this.setData({
            obj: res
        })
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})